<!--
 * @Descripttion: (产能报表/cks_capacityreport)
 * @Author: (admin)
 * @Date: (2025-07-01)
-->
<template>
    <div>
      <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
        
      <el-form-item label="时间">
        <!--  :default-time="defaultTime2" -->
        <el-date-picker v-model="queryParams.dateRange" style="width: 360px" type="daterange"
          :default-time="defaultTime2" value-format="YYYY-MM-DD HH:mm:ss" range-separator="-" start-placeholder=""
          end-placeholder=""></el-date-picker>
      </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-form>
      <!-- 工具区域 -->
      <el-row :gutter="15" class="mb10">
        <el-col :span="1.5">
          <!-- <el-button type="primary" v-hasPermi="['ckscapacityreport:add']" plain icon="plus" @click="handleAdd">
            {{ $t('btn.add') }}
          </el-button> -->
        </el-col>
        <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
      </el-row>
  
      <el-table
        :data="dataList"
        v-loading="loading"
        ref="table"
        border
        header-cell-class-name="el-table-header-cell"
        highlight-current-row
        @sort-change="sortChange"
        >
        <el-table-column type="index" :formatter="formatIndex" label="#" width="90" align="center"/>
        <el-table-column prop="statDayKey" label="统计日期" align="center"  />
        <el-table-column prop="inputNum" label="投入数量" align="center" />
        <el-table-column prop="output" label="产出数量" align="center" />
        <el-table-column prop="yieldRate" label="良率" :formatter="formatPercent" align="center" />
        
        
      </el-table>
      <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  
  
      <!-- 添加或修改产能报表对话框 -->
      <el-dialog :title="title" :lock-scroll="false" v-model="open" >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
          <el-row :gutter="20">
              
            <el-col :lg="12" v-if="opertype != 1">
              <el-form-item label="Id" prop="id">
                <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id" :disabled="true"/>
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="统计日期" prop="statDate">
                <el-date-picker v-model="form.statDate" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
              </el-form-item>
            </el-col>
              
            <el-col :lg="12">
              <el-form-item label="投入数量" prop="inputNum">
                <el-input v-model.number="form.inputNum" placeholder="请输入投入数量" />
              </el-form-item>
            </el-col>
              
            <el-col :lg="12">
              <el-form-item label="产出数量" prop="output">
                <el-input v-model.number="form.output" placeholder="请输入产出数量" />
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="良率" prop="yieldRate">
                <el-input v-model="form.yieldRate" placeholder="请输入良率" />
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="禁用状态" prop="nullity">
                <el-input v-model="form.nullity" placeholder="请输入禁用状态" />
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="创建时间" prop="createdTime">
                <el-date-picker v-model="form.createdTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="是否删除" prop="isDeleted">
                <el-radio-group v-model="form.isDeleted">
                  <el-radio v-for="item in options.isDeletedOptions" :key="item.dictValue" :value="item.dictValue">
                    {{item.dictLabel}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer v-if="opertype != 3">
          <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
          <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="ckscapacityreport">
  import { listCksCapacityreport,
   addCksCapacityreport, delCksCapacityreport, 
   updateCksCapacityreport,getCksCapacityreport, 
   } 
  from '@/api/business/ckscapacityreport.js'
  const { proxy } = getCurrentInstance()
  const ids = ref([])
  const loading = ref(false)
  const showSearch = ref(true)
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    sort: '',
    sortType: 'asc',
    dateRange:[  ],
  })
  const columns = ref([
    { visible: true, prop: 'id', label: 'Id' },
    { visible: true, prop: 'statDate', label: '统计日期' },
    { visible: true, prop: 'inputNum', label: '投入数量' },
    { visible: true, prop: 'output', label: '产出数量' },
    { visible: true, prop: 'yieldRate', label: '良率' },
    { visible: true, prop: 'nullity', label: '禁用状态' },
    { visible: true, prop: 'createdTime', label: '创建时间' },
    { visible: true, prop: 'isDeleted', label: '是否删除' },
  ])
  const total = ref(0)
  const dataList = ref([])
  const queryRef = ref()
  const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
  
  
  var dictParams = [
  ]
  const defaultTime2 = [
    new Date(2000, 1, 1, 0, 0, 0),
    new Date(2000, 2, 1, 23, 59, 59),
] 
  const formatPercent = (row, column, value) => {
    return `${(value * 100).toFixed(2)}%`; // 保留两位小数
    }

// 自定义行号（支持分页时连续编号）
const formatIndex = (row, column, cellValue, index) => {

return index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize;
}


function getQueryBody()
{
  
    const { dateRange ,pageNum,pageSize } = queryParams
    const body = { pageNum,pageSize }
    proxy.addDateRange(body, dateRange) 
    return body
}

  function getList(){
    loading.value = true
    listCksCapacityreport(getQueryBody()).then(res => {
      const { code, data } = res
      if (code == 200) {
        dataList.value = data.result
        total.value = data.totalNum
        loading.value = false
      }
    })
  }
  
  // 查询
  function handleQuery() {
    queryParams.pageNum = 1
    getList()
  }
  
  // 重置查询操作
  function resetQuery(){
    proxy.resetForm("queryRef")
    queryParams.dateRange = []
    handleQuery()
  }
  // 自定义排序
  function sortChange(column) {
    var sort = undefined
    var sortType = undefined
  
    if (column.prop != null && column.order != null) {
      sort = column.prop
      sortType = column.order
  
    }
    queryParams.sort = sort
    queryParams.sortType = sortType
    handleQuery()
  }
  
  /*************** form操作 ***************/
  const formRef = ref()
  const title = ref('')
  // 操作类型 1、add 2、edit 3、view
  const opertype = ref(0)
  const open = ref(false)
  const state = reactive({
    single: true,
    multiple: true,
    form: {},
    rules: {
      statDate: [{ required: true, message: "统计日期不能为空", trigger: "blur"     }],
      inputNum: [{ required: true, message: "投入数量不能为空", trigger: "blur"    , type: "number"  }],
      output: [{ required: true, message: "产出数量不能为空", trigger: "blur"    , type: "number"  }],
      yieldRate: [{ required: true, message: "良率不能为空", trigger: "blur"     }],
      nullity: [{ required: true, message: "禁用状态不能为空", trigger: "blur"     }],
      createdTime: [{ required: true, message: "创建时间不能为空", trigger: "blur"     }],
      isDeleted: [{ required: true, message: "是否删除不能为空", trigger: "blur"     }],
    },
    options: {
      // 是否删除 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
  isDeletedOptions: [],
    }
  })
  
  const { form, rules, options, single, multiple } = toRefs(state)
  
  // 关闭dialog
  function cancel(){
    open.value = false
    reset()
  }
  
  // 重置表单
  function reset() {
    form.value = {
      id: null,
      statDate: null,
      inputNum: null,
      output: null,
      yieldRate: null,
      nullity: null,
      createdTime: null,
      isDeleted: null,
    };
    proxy.resetForm("formRef")
  }
  
  
  // 添加按钮操作
  function handleAdd() {
    reset();
    open.value = true
    title.value = '添加产能报表'
    opertype.value = 1
  }
  // 修改按钮操作
  function handleUpdate(row) {
    reset()
    const id = row.id || ids.value
    getCksCapacityreport(id).then((res) => {
      const { code, data } = res
      if (code == 200) {
        open.value = true
        title.value = '修改产能报表'
        opertype.value = 2
  
        form.value = {
          ...data,
        }
      }
    })
  }
  
  // 添加&修改 表单提交
  function submitForm() {
    proxy.$refs["formRef"].validate((valid) => {
      if (valid) {
  
        if (form.value.id != undefined && opertype.value === 2) {
          updateCksCapacityreport(form.value).then((res) => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          })
        } else {
          addCksCapacityreport(form.value).then((res) => {
              proxy.$modal.msgSuccess("新增成功")
              open.value = false
              getList()
            })
        }
      }
    })
  }
  
  // 删除按钮操作
  function handleDelete(row) {
    const Ids = row.id || ids.value
  
    proxy
      .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
        confirmButtonText: proxy.$t('common.ok'),
        cancelButtonText: proxy.$t('common.cancel'),
        type: "warning",
      })
      .then(function () {
        return delCksCapacityreport(Ids)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess("删除成功")
      })
  }
  
  
  
  
  
  handleQuery()
  </script>